from django.db import models

# Create your models here.

from django.db import models

from Apps.Project.models import Project
from Apps.common.custommodel import CustomModel, ModifyModel
from Apps.User.models import User

import logging

from Apps.util.snowFlake import get_guid

logger = logging.getLogger('django')


class InterfaceCat(CustomModel):
    label = models.CharField(max_length=40, verbose_name="接口模块名称", help_text="接口模块名称")
    index = models.CharField(max_length=20, default=get_guid, verbose_name="模块排序", null=True)
    parentId = models.CharField(max_length=20, verbose_name="上级模块", default="", null=True, blank=True)
    project = models.ForeignKey(to=Project, db_constraint=False, related_name="interface_interface_cat_project",
                                on_delete=models.SET_NULL, verbose_name="项目", null=True)

    class Meta:
        db_table = 'interface_interface_cat'
        verbose_name = db_table
        ordering = ('-index',)

    def __str__(self):
        return self.label


class InterfaceStatus(CustomModel):
    name = models.CharField(max_length=50, verbose_name='状态名称')

    is_active = models.BooleanField(default=True, verbose_name='是否启用中')
    # 禁用的状态只是不能再选择，但是已选择的数据，不做更改

    creator = models.ForeignKey(to=User, db_constraint=False, related_name="interface_interface_status_creator",
                                on_delete=models.SET_NULL, verbose_name="创建人", null=True)
    modifier = models.ForeignKey(to=User, db_constraint=False, related_name="interface_interface_status_modifier",
                                 on_delete=models.SET_NULL, verbose_name="修改人", null=True)
    project = models.ForeignKey(to=Project, db_constraint=False, related_name="interface_interface_status_project",
                                on_delete=models.SET_NULL, verbose_name="项目", null=True)

    class Meta:
        db_table = 'interface_interface_status'
        verbose_name = db_table
        ordering = ('-create_time',)

    def __str__(self):
        return self.name


class InterfaceTest(models.Model):
    # id = models.CharField(max_length=50, primary_key=True, default=uuid.uuid4, help_text="Id", verbose_name="Id")
    id = models.CharField(max_length=20, primary_key=True, help_text="Id",
                          verbose_name="Id")
    create_time = models.DateTimeField(auto_now_add=True, db_index=True, verbose_name='创建时间')
    update_time = models.DateTimeField(auto_now=True, db_index=True, verbose_name='修改时间', null=True)

    path = models.CharField(max_length=255, verbose_name="请求路径", null=True, help_text="请求路径")
    method = models.CharField(max_length=10, verbose_name="请求方式", null=True, help_text="请求方式")
    name = models.CharField(max_length=40, verbose_name="接口名称", help_text="接口名称")
    index = models.CharField(max_length=20, default=get_guid, verbose_name="排序", null=True)
    status = models.ForeignKey(to=InterfaceStatus, db_constraint=False, related_name="apitest_api_status",
                               on_delete=models.SET_NULL, verbose_name="状态", null=True)
    responsibleId = models.ForeignKey(User, verbose_name="责任人", related_name="aiptest_api_responsibleId",
                                      on_delete=models.DO_NOTHING, null=True)
    tags = models.JSONField(default=list, verbose_name="标签", help_text="标签", null=True)

    description = models.TextField(verbose_name="描述", null=True)

    responses = models.JSONField(default=list, verbose_name="响应定义", help_text="响应定义", null=True)

    TYPE_CHOICES = (
        (1, "http"),
        (2, "websocket"),
    )
    type = models.IntegerField(default=1, choices=TYPE_CHOICES, verbose_name="请求类型", help_text="请求类型")

    parameters = models.JSONField(default=dict, verbose_name="Params,Cookie,Header", null=True)

    requestBody = models.JSONField(default=dict, verbose_name="请求体", null=True)
    auth = models.JSONField(default=dict, verbose_name="权限认证", null=True)
    advancedSettings = models.JSONField(default=dict, verbose_name="设置", null=True)
    preProcessors = models.JSONField(default=dict, verbose_name="前置操作", null=True)
    postProcessors = models.JSONField(default=dict, verbose_name="后置操作", null=True)
    is_deleted = models.BooleanField(default=False, verbose_name="是否已删除", help_text="是否已删除")

    creator = models.ForeignKey(to=User, db_constraint=False, related_name="interface_interface_test_creator",
                                on_delete=models.SET_NULL, verbose_name="创建人", null=True)
    modifier = models.ForeignKey(to=User, db_constraint=False, related_name="interface_interface_test_modifier",
                                 on_delete=models.SET_NULL, verbose_name="修改人", null=True)
    cat = models.ForeignKey(to=InterfaceCat, db_constraint=False, related_name="interface_interface_test_cat",
                            on_delete=models.SET_NULL, verbose_name="接口模块", null=True)

    project = models.ForeignKey(to=Project, db_constraint=False, related_name="interface_interface_test_project",
                                on_delete=models.SET_NULL, verbose_name="项目", null=True)

    class Meta:
        db_table = 'interface_interface_test'
        verbose_name = db_table
        ordering = ('-create_time',)

    def __str__(self):
        return self.name


class InterfaceTestRequestLog(CustomModel):
    path = models.CharField(max_length=255, verbose_name="请求路径", null=True, help_text="请求路径")
    method = models.CharField(max_length=10, verbose_name="请求方式", null=True, help_text="请求方式")
    name = models.CharField(max_length=40, verbose_name="接口名称", help_text="接口名称")
    TYPE_CHOICES = (
        (1, "http"),
        (2, "websocket"),
    )
    type = models.IntegerField(default=1, choices=TYPE_CHOICES, verbose_name="请求类型", help_text="请求类型")

    parameters = models.JSONField(default=dict, verbose_name="Params,Cookie,Header", null=True)

    requestBody = models.JSONField(default=dict, verbose_name="请求体", null=True)
    auth = models.JSONField(default=dict, verbose_name="权限认证", null=True)
    advancedSettings = models.JSONField(default=dict, verbose_name="设置", null=True)
    preProcessors = models.JSONField(default=dict, verbose_name="前置操作", null=True)
    postProcessors = models.JSONField(default=dict, verbose_name="后置操作", null=True)

    # 响应
    response_time = models.FloatField(default=0, verbose_name="响应时间")
    response_status = models.CharField(default="", max_length=10, verbose_name="status")
    response_size = models.IntegerField(default=0, verbose_name="response_size")
    response_headers = models.JSONField(default=dict, verbose_name="response_headers")
    response_body = models.TextField(default="", verbose_name="响应体")
    console_out = models.TextField(default="", verbose_name="控制台输出")

    request_user = models.ForeignKey(to=User, db_constraint=False, verbose_name="执行接口请求操作的人",
                                     on_delete=models.SET_NULL, null=True,
                                     related_name="interface_interface_test_request_log_user")
    interface = models.ForeignKey(to=InterfaceTest, db_constraint=False, on_delete=models.SET_NULL, null=True)

    class Meta:
        db_table = 'interface_interface_test_request_log'
        verbose_name = db_table
        ordering = ('-create_time',)

    def __str__(self):
        return self.name


# 全局变量，保存使用
class InterfaceVariableEnv(CustomModel):
    id = models.CharField(max_length=20, primary_key=True, default=get_guid, help_text="Id", verbose_name="Id")
    env_name = models.CharField(max_length=40, default="", verbose_name="环境名称", help_text="环境名称")
    env_value = models.JSONField(default=list, verbose_name="全局变量数据", help_text="全局变量数据")
    """
    [{
    "key": "", # 全局变量名
    "value":"", # 全局变量值
    "value_type":"", # 全局变量值类型
    "description":"" # 描述
    }]
    """
    project = models.ForeignKey(to=Project, db_constraint=False, related_name="interface_variable_env_project",
                                on_delete=models.SET_NULL, verbose_name="项目", null=True)

    class Meta:
        db_table = 'interface_variable_env'
        verbose_name = db_table
